package com.airline.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.airline.beans.Employee;
import com.airline.connection.AirlineSystemServiceProxy;
import com.airline.form.LoginForm;

public class EmployeeLoginAction extends Action {
	
	AirlineSystemServiceProxy proxy = new AirlineSystemServiceProxy();

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		LoginForm employeeForm = (LoginForm) form;
		

		String username = employeeForm.getUsername();
		String pwd = employeeForm.getPassword();

		Employee emp = proxy.employeeLogin(username,pwd);


		if (null == emp) {
			employeeForm.setError("Either Username or Password entered is wrong");
			return mapping.findForward("failure");
		}
		HttpSession httpsess = request.getSession();
		httpsess.setAttribute("employee", emp);
		httpsess.setAttribute("tempemp", emp);
		
		if(emp.getPosition().equalsIgnoreCase("Admin"))

		return mapping.findForward("success");
		else
			return mapping.findForward("notadmin");
	}

	

}
